<?php

require_once(DIR_LIB."VsGui".DIR_SEP."vssmartywidget.php");

require_once(dirname(__FILE__).DIR_SEP."vsdateedit.php");

require_once(DIR_LIB."Junona".DIR_SEP."junobjectsfactory.php");
require_once(DIR_LIB."Junona".DIR_SEP."junoperatorwork.php");

class XCartOperationsWorkCount extends VsSmartyWidget {

	private $_products = array();

	function __construct(&$page, $modelId, $forPrint=FALSE, $pager=NULL) {
		// Ne go zabravqj ina4e nqma $smarty Object
		parent::__construct();

		if($forPrint) {
			$this->_template = dirname(__FILE__).DIR_SEP."xcart_operations_work_count_print.html";
		}
		else {
			$this->_template = dirname(__FILE__).DIR_SEP."xcart_operations_work_count.html";
		}

		$this->_wwwDir = 'templates/XCart/';

		$page->addJsFile($this->_wwwDir."js/vsprint.js");
/*
		$page->addCssFile($this->_wwwDir."css/skin1_admin.css");
		$page->addCssFile($this->_wwwDir."css/switcher.css");
		$page->addJsFile($this->_wwwDir."js/common.js");
		$page->addJsFile($this->_wwwDir."js/switcher.js");
*/

		$minDate = isset($_POST['fromVsdeDate']) ? $_POST['fromVsdeDate'] : date("Y-m-d");
		$maxDate = isset($_POST['toVsdeDate']) ? $_POST['toVsdeDate'] : date("Y-m-d");
		//echo "FROM: ".$minDate."  TO:  ".$maxDate;
		//echo "FROM: ".$_POST['fromVsdeDate']."  TO:  ".$_POST['toVsdeDate'];

		
		if($forPrint) {
			$fromDate = $_REQUEST['fromVsdeDate'];
			$toDate = $_REQUEST['toVsdeDate'];
			
			
			$this->assign('fromVsdeDate',  $fromDate);
			$this->assign('toVsdeDate',  $toDate);
		}
		else {
			$fromDateEdit = new VsDateEdit($page, $minDate);
			$fromDate = $fromDateEdit->date();
			$this->assign('fromDateEdit',  $fromDateEdit->show());
			$this->assign('fromVsdeDate',  $fromDate);

			$toDateEdit = new VsDateEdit($page, $maxDate);
			$toDate = $toDateEdit->date();
			$this->assign('toDateEdit',  $toDateEdit->show());
			$this->assign('toVsdeDate',  $toDate);
		
		
		
		}

		if(isset($_POST['sub_operation'])) {
			$this->procForm();
		}

		if(isset($_POST['sub_operations'])) {
			$this->procFormOperations();
		}
		
		$model = JunObjectsFactory::makeModel($modelId);
		$operations = JunObjectsFactory::makeOperationsWorkCount($modelId, $fromDate, $toDate);

		$this->assign('model', $model);
		$this->assign('listOperations', $operations[0]);
		$this->assign('subTotal', $operations[1]);

		if($pager)
			$this->assign('pager', $pager->show());
		else
			$this->assign('pager', '');
	}
	//============================================================================

	function show() {
		$this->assign('wwwDir', $this->_wwwDir);

		return $this->fetch($this->_template);
	}
	//============================================================================

	private function procForm() {
		$model = JunOperation::makeFromForm($_POST);
		$model->save();

		header("Location: ".$_SERVER['PHP_SELF']."?modid=".$_REQUEST['modid']);
	}
	//===========================================================================

	private function procFormOperations() {
		//print_r($_POST['posted_data']);
		foreach($_POST['opids'] as $id=>$value) {
			JunOperation::changePrice($id, $_POST['posted_data'][$id]['price']);

			if(isset($_POST['change_work']))
				JunOperation::updateWorkPrices($id, $_POST['posted_data'][$id]['price'], $_POST['fromVsdeDate'], $_POST['toVsdeDate']);
		}

		header("Location: ".$_SERVER['PHP_SELF']."?modid=".$_REQUEST['modid']);
	}
	//===========================================================================


	private function procFormAddWork() {
		$work = JunOperatorWork::makeForAdd($_POST['opid'], $_POST['vsdeDate']);

		foreach($_POST['opids'] as $id=>$value) {
			$op = JunObjectsFactory::makeOperation($id);

			$work->addOperation($op, $_POST['posted_data'][$id]['count'], $_POST['vsdeDate']);

			//$p->setPrice($_POST['posted_data'][$id]['price']);
		}

		$work->save();

		header("Location: ".$_SERVER['PHP_SELF']."?opid=".$_REQUEST['opid']);		//."?modid=".$_REQUEST['modid']);
	}
	//============================================================================

};

?>
